package com.ebao.action.user;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.ebao.action.base.BaseAction;
import com.ebao.constants.EmailType;
import com.ebao.constants.SendType;
import com.ebao.constants.UserType;
import com.ebao.po.mail.TEmailRecord;
import com.ebao.po.user.TUserRegister;
import com.ebao.service.mail.IEmailRecordService;
import com.ebao.service.user.IUserRegisterService;
import com.ebao.util.PageResult;
import com.ebao.util.encrypt.MD5;
import com.ebao.util.mail.EmailSender;

/**
 * @author snow
 * @date 2012-7-18
 */
public class UserRegisterAction extends BaseAction {

	private static final long serialVersionUID = -6799977600583837827L;

	@Resource
	private IUserRegisterService userRegisterService;
	@Resource
	private IEmailRecordService emailRecordService;
	
	private String id;
	private String info;	// json返回的信息
	private PageResult<TUserRegister> pageResults;
	private String page;	// 当前页数
	private String pageSize;// 
	private String status;	// 状态
	
	/**
	 * 查看注册申请
	 * status : 0未审核,1审核通过,2审核未通过
	 */
	@Action(value = "list-register", results = {@Result(name = SUCCESS, location = "listRegister.jsp")})
	public String list() {
		int theStatus = (status == null) ? 0 : Integer.parseInt(status);
		String jpql = "from TUserRegister r where r.status=?";
		log.debug(jpql);
		int thePageNo = (page == null) ? 1 : (Integer.parseInt(page) < 1 ? 1 : Integer.parseInt(page));
		int thePageSize = (pageSize == null) ? 10 : (Integer.parseInt(pageSize) < 1 ? 10 : Integer.parseInt(pageSize));
		pageResults = userRegisterService.getPageByJpql(jpql, thePageNo, thePageSize, theStatus);
		request.put("url", "list-register.do?status=" + theStatus);
		return SUCCESS;
	}
	
	/**
	 * 审核通过,邮件通知用户/医生
	 */
	@Action(value = "pass-register", results = {@Result(name = SUCCESS, 
			type = "json", params = {"root", "info"})})
	public String pass() {
		TUserRegister userRegister = userRegisterService.getById(Long.parseLong(id));
		if(userRegister.getStatus() != 0) {
			info = "已审核过，无需再次审核";
			return SUCCESS;
		}
		
		userRegister.setStatus(1);
		userRegisterService.update(userRegister);
		
		// 发送邮件
		String id = userRegister.getId() + "";
		String mail = userRegister.getEmail();
		String email = MD5.MD5Encode(mail);
		String subject = "恭喜您加入医宝网";
		TEmailRecord emailRecord = new TEmailRecord();
		emailRecord.setEmailFrom("system@ebao.com");
		emailRecord.setEmailTo(userRegister.getEmail());
		emailRecord.setSubject(subject);
		emailRecord.setType(EmailType.OTHER);
		String url = "http://localhost:8080/ebao";		// 激活账户URL
		if (userRegister.getUserType() == UserType.COMMON_USER) {// 用户
			url += "/user/user-confirmRegister.html?encrypt=" + email + "&id="+ id;
			emailRecord.setToType(SendType.USER);
			
		} else if (userRegister.getUserType() == UserType.DOCTOR) {// 医生
			url += "/user/doctor-confirmRegister.html?encrypt=" + email + "&id="+ id;
			emailRecord.setToType(SendType.DOCTOR);
		}
		String content = "请点击链接激活账户&nbsp;<a href='" + url + "'>" + url + "</a>";
		// 发送邮件
		EmailSender.send(mail, subject, content, "text/html");
		emailRecord.setContent(content);
		emailRecord.setHassent(true);
		emailRecord.setSendDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		// save emailRecord
		emailRecordService.save(emailRecord);
		
		info = "操作成功";
		
		return SUCCESS;
	}

	/**
	 * 审核不通过
	 */
	@Action(value = "not-pass-register", results = {@Result(name = SUCCESS, 
			type = "json", params = {"root", "info"})})
	public String notPass() {
		TUserRegister userRegister = userRegisterService.getById(Long.parseLong(id));
		if(userRegister.getStatus() != 0) {
			info = "已审核过，无需再次审核";
			return SUCCESS;
		}
		
		userRegister.setStatus(2);
		userRegisterService.update(userRegister);
		info = "操作成功";
		return SUCCESS;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public PageResult<TUserRegister> getPageResults() {
		return pageResults;
	}

	public void setPageResults(PageResult<TUserRegister> pageResults) {
		this.pageResults = pageResults;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

}
